package com.csaba.swing.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.csaba.connector.model.Session;
import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.util.TableSizeContainer;
import com.csaba.swing.gui.util.WindowSizeContanier;

@SuppressWarnings( "serial" )
public class SessionListView extends JInternalFrame implements ContextListener
{
	static String[] HEADERS = new String[] { MainWindow.getString("SessionListView.header.bank"), MainWindow.getString("SessionListView.header.customer"), MainWindow.getString("SessionListView.header.loginId") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	static Class<?>[] COLUMN_CLASS = new Class<?>[] { ImageIcon.class, String.class, String.class };

	JTable sessions;

	public SessionListView()
	{
		super(MainWindow.getString("SessionListView.title"), true, true, true, true); //$NON-NLS-1$
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		if ( !WindowSizeContanier.getInstance().monitorComponent(this) )
			setSize(400, 350);
		//setFrameIcon(GUIContext.getIcon("???", 16));

		//init layout
		final JPanel general = new JPanel();
		general.add(new JLabel(MainWindow.getString("SessionListView.msg.doubleClick"))); //$NON-NLS-1$

		final JPanel buttons = new JPanel();
		buttons.add(new JButton(new AbstractAction(MainWindow.getString("SessionListView.button.close")) //$NON-NLS-1$
		{

			@Override
			public void actionPerformed( final ActionEvent arg0 )
			{
				dispose();
			}

		}));

		sessions = new JTable(new Object[0][2], new String[] { MainWindow.getString("SessionListView.header.name"), MainWindow.getString("SessionListView.header.value") }); //$NON-NLS-1$ //$NON-NLS-2$
		final JScrollPane scrollPane = new JScrollPane(sessions);
		sessions.setFillsViewportHeight(true);
		sessions.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked( final MouseEvent e )
			{
				if ( e.getClickCount() > 1 )
				{//handle as double click
					final int index = sessions.getSelectedRow();
					final Session session = ( (SessionListModel) sessions.getModel() ).getSessionAt(index);
					final CustomerView view = new CustomerView();
					MainWindow.getDesktop().add(view);
					view.showIt(session.getCustomer());
				}

			}

		});

		final JPanel contentPane = (JPanel) getContentPane();
		contentPane.setBorder(BorderFactory.createEmptyBorder());
		contentPane.setLayout(new BorderLayout());
		contentPane.add(general, BorderLayout.NORTH);
		contentPane.add(buttons, BorderLayout.SOUTH);
		contentPane.add(scrollPane, BorderLayout.CENTER);
		GUIContext.getInstance().addContextListener(this);
	}

	public void showIt( final Session[] sessions )
	{
		this.sessions.setModel(new SessionListModel(sessions));

		setVisible(true);
		TableSizeContainer.getInstance().monitorTable("SessionListView.sessions", this.sessions); //$NON-NLS-1$
	}

	private class SessionListModel extends AbstractTableModel
	{
		private final Session[] sessions;

		public SessionListModel()
		{
			super();
			this.sessions = new Session[0];
		}

		public SessionListModel( final Session[] sessions )
		{
			super();
			this.sessions = sessions;
		}

		@Override
		public int getColumnCount()
		{
			return HEADERS.length;
		}

		@Override
		public int getRowCount()
		{
			return sessions.length;
		}

		@Override
		public Class<?> getColumnClass( final int columnIndex )
		{
			return COLUMN_CLASS[columnIndex];
		}

		@Override
		public Object getValueAt( final int rowIndex, final int columnIndex )
		{
			final Session session = sessions[rowIndex];
			Object result = null;
			switch ( columnIndex )
			{
			case 0:
				final URL icon = session.getBank().getSmallIcon();
				if ( icon != null )
					result = GUIContext.getIcon(icon);
				break;
			case 1:
				if ( session.getCustomer() != null )
					result = session.getCustomer().getName();
				else
					result = MainWindow.getString("SessionListView.notLoggedIn"); //$NON-NLS-1$
				break;
			case 2:
				if ( session.getCustomer() != null )
					result = session.getCustomer().getLoginId();
				else
					result = ""; //$NON-NLS-1$
				break;
			}
			return result;
		}

		public Session getSessionAt( final int row )
		{
			return sessions[row];
		}

		@Override
		public boolean isCellEditable( final int rowIndex, final int columnIndex )
		{
			// this is a read only model
			return false;
		}

		@Override
		public String getColumnName( final int column )
		{
			return HEADERS[column];
		}

	}

	@Override
	public void contextEventPerformed( final int eventType )
	{
		final GUIContext context = GUIContext.getInstance();
		if ( context.hasActiveSession() )
		{
			final Session[] sessions = context.getSessions().toArray(new Session[context.getSessions().size()]);
			showIt(sessions);
		}
		else
		{
			dispose();
		}

	}
}
